package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.*;
import com.atguigu.gmall.pms.entity.vo.SkuVo;
import com.atguigu.gmall.pms.entity.vo.SpuAttrValueVo;
import com.atguigu.gmall.pms.entity.vo.SpuVo;
import com.atguigu.gmall.pms.mapper.SkuMapper;
import com.atguigu.gmall.pms.mapper.SpuDescMapper;
import com.atguigu.gmall.pms.service.SkuAttrValueService;
import com.atguigu.gmall.pms.service.SkuImagesService;
import com.atguigu.gmall.pms.service.SpuAttrValueService;
import com.atguigu.gmall.pms.service.feign.GmallSmsClient;
import com.atguigu.gmall.sms.vo.SkuSaleVo;
import io.jsonwebtoken.lang.Collections;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.SpuMapper;
import com.atguigu.gmall.pms.service.SpuService;
import org.springframework.transaction.annotation.Transactional;

import static org.springframework.beans.BeanUtils.copyProperties;


@Service("spuService")

public class SpuServiceImpl extends ServiceImpl<SpuMapper, SpuEntity> implements SpuService {

    @Autowired
    SpuDescMapper spuDescMapper;
    @Autowired
    SkuMapper skuMapper;

    @Autowired
    SpuAttrValueService spuAttrValueService;
    @Autowired
    SkuImagesService skuImagesService;
    @Autowired
    SkuAttrValueService skuAttrValueService;
    @Autowired
    GmallSmsClient gmallSmsClient;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SpuEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SpuEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public PageResultVo querySpuInfo(PageParamVo paramVo, Long categoryId) {
        QueryWrapper<SpuEntity> queryWrapper = new QueryWrapper<>();
        if(categoryId!=0){
            queryWrapper.eq("category_id",categoryId);
        }
        String key = paramVo.getKey();
        if(!StringUtils.isBlank(key)){
            queryWrapper.and(t->t.like("name",key).or().like("id",key));
        }
        return new PageResultVo(this.page(paramVo.getPage(),queryWrapper));
    }
    @GlobalTransactional
    @Override
    public void bigsave(SpuVo spuVo) {

        //存储spu信息
        this.saveSpuInfo(spuVo);
        //desc存图片详情,地址以逗号分割
        this.saveSpuDesc(spuVo);
        //baseAttrs
        int i = 1/0;
        this.saveBaseAttrs(spuVo);
        //sku信息
        //sku表
        this.saveSkuInfo(spuVo);
    }
    @Transactional
    public void saveSpuInfo(SpuVo spuVo){
        spuVo.setPublishStatus(1);
        spuVo.setCreateTime(new Date());
        spuVo.setUpdateTime(spuVo.getCreateTime());
        SpuEntity spuEntity = new SpuEntity();
        copyProperties(spuVo,spuEntity);
        this.save(spuEntity);
        //spuId返回给spuVo
        spuVo.setId(spuEntity.getId());
    }
    @Transactional
    public void saveSpuDesc(SpuVo spuVo){
        SpuDescEntity spuDescEntity = new SpuDescEntity();
        spuDescEntity.setSpuId(spuVo.getId());
        spuDescEntity.setDecript(StringUtils.join(spuVo.getSpuImages(),","));
        spuDescMapper.insert(spuDescEntity);
    }
    @Transactional
    public void saveBaseAttrs(SpuVo spuVo){
        List<SpuAttrValueVo> baseAttrs = spuVo.getBaseAttrs();
        if(!Collections.isEmpty(baseAttrs)){
            List<SpuAttrValueEntity> spuAttrValueEntities = new ArrayList<>();
            baseAttrs.forEach(spuAttrValueVo -> {
                spuAttrValueVo.setSpuId(spuVo.getId());
                spuAttrValueVo.setSort(0);
                spuAttrValueEntities.add(spuAttrValueVo);
            });
            spuAttrValueService.saveBatch(spuAttrValueEntities);
        }
    }
    @Transactional
    public void saveSkuInfo(SpuVo spuVo){
        List<SkuVo> skus = spuVo.getSkus();
        skus.forEach(skuVo -> {
            SkuEntity skuEntity = new SkuEntity();
            copyProperties(skuVo,skuEntity);
            skuEntity.setSpuId(spuVo.getId());
            skuEntity.setBrandId(spuVo.getBrandId());
            skuEntity.setCategoryId(spuVo.getCategoryId());
            //获取图片集
            List<String> images = skuVo.getImages();
            //设置默认封面
            if(!Collections.isEmpty(images)){
                skuEntity.setDefaultImage(skuEntity.getDefaultImage()==null ? images.get(0) : skuEntity.getDefaultImage());
            }
            skuMapper.insert(skuEntity);
            Long skuId = skuEntity.getId();
            //存图片信息到 sku_images
            if(!Collections.isEmpty(images)){
                List<SkuImagesEntity> skuImages = new ArrayList<>();
                String defaultImage = images.get(0);
                images.forEach(image->{
                    SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                    skuImagesEntity.setSkuId(skuId);
                    skuImagesEntity.setSort(0);
                    skuImagesEntity.setUrl(image);
                    skuImagesEntity.setDefaultStatus(image.equals(defaultImage)? 1:0);
                });
                skuImagesService.saveBatch(skuImages);
            }
            //存sku属性信息到sku_attr
            List<SkuAttrValueEntity> saleAttrs = skuVo.getSaleAttrs();
            saleAttrs.forEach(skuAttrValueEntity -> {
                skuAttrValueEntity.setSkuId(skuId);
                skuAttrValueEntity.setSort(0);
            });
            skuAttrValueService.saveBatch(saleAttrs);
            //远程调用
            //积分活动bound
            SkuSaleVo skuSaleVo = new SkuSaleVo();
            BeanUtils.copyProperties(skuVo,skuSaleVo);
            skuSaleVo.setSkuId(skuId);
            gmallSmsClient.saveSkuSale(skuSaleVo);
        });
    }
}